﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebAPITest.Models;
using WebAPITest.Models.Database;
using WebAPITest.service;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //注入jwt服务
        private readonly IJWTService _jwtService;
        private readonly WebEnterpriseIIContext _db;
        private readonly IUserService _userService;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService, WebEnterpriseIIContext db,IUserService userService)
        {
            _jwtService = jwtService;
            _db = db;
            _userService = userService;
        }
        
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            //如果是登录
            //判断用户名和密码是否正确

            //如果正确才创建token 用这个用户名的用户信息去创建token 

            return _jwtService.CreateToken(name,0,"","");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        
        public object UserInfo()
        {
            //获取用户信息
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var user = _db.Users.FirstOrDefault(x => x.UserId == Convert.ToInt32(userId));
            return user;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        

        public string Reg(RegRequest request)
        {
            //保证用户名唯一
            //判断数据（用户名为request.UserName)是否存在
            //查询用户表是否有存在的用户名
            if (_userService.ExixtUserByUserName(request.UserName))
                return "用户存在";

            //var user = new User()
            //{
            //    UserName = request.UserName,
            //    Email = request.Email,
            //    NickName = request.NickName,
            //    Password = request.Password,
            //    CreateTime = DateTime.Now,
            //    Salt = 0,
            //    UserLevel = 1

            //};
            //_db.Users.Add(user);
            //var row=_db.SaveChanges();
            var row = _userService.AddNewUser(request);
            if (row > 0)
            {
                return "注册成功";
            }
            return "注册失败";
        }
        [HttpPost]
        public string Login(string username, string password)
        {
            //第一步 从数据库查询用户为username的用户
            //var user = _db.Users.FirstOrDefault(x => x.UserName == username);


            var user = _userService.GetUserByUserName(username);
            //如果不存在用户就不存在
            //if (user == null)
            //{
            //    return "用户不存在";
            //}

            //如果不存在用户就不存在
            if (user == null)
            {
                return "用户不存在";
            }
            ;
            //如果存在 判断查询出来的用户密码是否跟password相等
            if (user.Password != password)
            {
                return "密码不正确";
            }
            //如果不相等 返回密码错误
            return _jwtService.CreateToken(user.UserName,user.UserId,user.Email,user.NickName);
            //如果相等？？？

            //创建Token 返回token给前端
        }

    }
}
